package com.vmladenov.cook.core.db;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.vmladenov.cook.core.GlobalStrings;
import com.vmladenov.cook.core.html.SmallPreview;
import com.vmladenov.cook.core.objects.UsefulView;

public class DictionaryRepository implements IRepository
{
	private final SQLiteDatabase db;

	SQLiteStatement maxIdStatement;

	private static final String MAX_ID_SQL = "SELECT MAX(ID) FROM "
			+ GlobalStrings.DICTIONARY_TABLE_NAME;

	private static final String DICTIONARY_SELECT_CLAUSE = "SELECT ID, IMAGE_URL, TITLE, DESCRIPTION "
			+ " FROM " + GlobalStrings.DICTIONARY_TABLE_NAME;

	private static final String DICTIONARY_PAGE_SQL = DICTIONARY_SELECT_CLAUSE + " LIMIT %d,10";

	private static final String DICTIONARY_SQL = DICTIONARY_SELECT_CLAUSE + " WHERE ID = %d";

	public DictionaryRepository(SQLiteDatabase db)
	{
		this.db = db;
		maxIdStatement = db.compileStatement(MAX_ID_SQL);
	}

	@Override
	public long getMaxId()
	{
		return maxIdStatement.simpleQueryForLong();
	}

	@Override
	public ArrayList<SmallPreview> getPreviews(long start)
	{
		String sql = String.format(DICTIONARY_PAGE_SQL, start);
		return getProductPreviews(sql);
	}

	private ArrayList<SmallPreview> getProductPreviews(String sql)
	{
		ArrayList<SmallPreview> previews = new ArrayList<SmallPreview>();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst())
		{
			do
			{
				SmallPreview preview = createProductPreview(cursor);
				previews.add(preview);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) cursor.close();
		return previews;
	}

	private SmallPreview createProductPreview(Cursor cursor)
	{
		SmallPreview preview = new SmallPreview();
		preview.setId(cursor.getLong(0));
		preview.setTitle(cursor.getString(2)); // Title
		String imageUrl = cursor.getString(1);
		if (imageUrl != null)
		{
			String imageName = imageUrl.substring(imageUrl.lastIndexOf('/') + 1);
			String smallImageUrl = GlobalStrings.DictionaryImagesUrlPrefix
					+ GlobalStrings.SmallImage100UrlPrefix + imageName;
			preview.setImageUrl(smallImageUrl);
			// try
			// {
			// preview.setCachedImage(Helpers.getSmallDictionaryImage(context,
			// imageName));
			// }
			// catch (IOException e)
			// {
			// e.printStackTrace();
			// }
		}
		return preview;
	}

	public void close()
	{
		maxIdStatement.close();
	}

	public UsefulView getDictionaryView(long id)
	{
		UsefulView view = new UsefulView();
		String sql = String.format(DICTIONARY_SQL, id);
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToFirst())
		{
			view.setId(id);
			view.setTitle(cursor.getString(2));
			view.setDescription(cursor.getString(3));
			view.setImageUrl(cursor.getString(1));
			// try
			// {
			// String imgUrl = cursor.getString(1);
			// if (cursor.getString(1) != null)
			// {
			// String imageName = imgUrl.substring(imgUrl.lastIndexOf('/') + 1);
			// view.setBigImage(Helpers.getSmallDictionaryImage(context,
			// imageName));
			// }
			// }
			// catch (IOException e)
			// {
			// }

			// String imgUrl = cursor.getString(1);
			// if (imgUrl != null)
			// {
			// view.setBigImage(Helpers.getImage(imgUrl));
			// }
		}
		if (cursor != null && !cursor.isClosed()) cursor.close();

		return view;
	}

}
